app.controller('loginController', function ($scope, $rootScope, $http, $interval, $compile,$state) {
	//登录
    $scope.login = function (type) {
		/*var reqeustData = {
            "username":"zhaojun@pcitech.com",
            "password":123456
        };*/
         /*var reqeustData = {
            "username":"17038268@qq.com",
            "password":123456
        };*/
          // var reqeustData = {"username":"807079151@qq.com","password":"123456"};

        /*if(1==type){
            $state.go(one.stateName);
        }else{
            $state.go(pv1_0.stateName); hfjzlmps@163.com/123456
        }*/
    	// context.appBlh("pvBlhResult");

        //如果有版本更新则不可使用
        // if($scope.versionFlag){
        //     $rootScope.tipPopups.show(($rootScope.tipMessage = $scope.updateInfo));
        //     return;
        // }

    	/*if(localStorage.getItem("username")){
    		$scope.user.username = localStorage.getItem("username");
    	}
    	if(localStorage.getItem("password")){
    		$scope.user.password = localStorage.getItem("password");
    	}*/
    	
        var reqeustData = {
            "username":$scope.user.username,
            "password":$scope.user.password
        };
        
//        if(!$scope.validData()){
//            return;
//        };
        
//        if($scope.user.username=='4552698@qq.com'||$scope.user.username=='hello@kucrm.com'){
//        	serverIP = "https://cnmpse2e-itg.ext.hp.com/fec-api/api/";
//        }else{
//        	serverIP = "https://mpse2e.ext.hp.com/fec-api/api/";
//        }
        
        postHttp($http, 'login', reqeustData, function (res) {
			if (res.msg === 'success') {
                saveToken(res.token);
                sessionStorage.setItem("username",reqeustData.username);
                localStorage.setItem("username",reqeustData.username);
                var type = res.role;
                sessionStorage.setItem("type", type);
                localStorage.setItem("type", type);

				//保存账号和密码
				if(checkPhone('android')){
                    AndroidMethod.saveUserPass(reqeustData.username,reqeustData.password);
                }else if(checkPhone("iphone")){
					// alert("save--"+reqeustData.username+","+reqeustData.password);
                    //context.appBlh();
					//context.saveUserPass(reqeustData.username,reqeustData.password);
					window.webkit.messageHandlers.saveUserPass.postMessage(reqeustData.username+"#"+reqeustData.password);
                }
				
                if(1==type){
                    $state.go(one.stateName);
                }else if(2==type){
                    $state.go(pv1_0.stateName);
                }else{
                    $state.go(choosePage.stateName);
                }
            } else {
                 $rootScope.tipPopups.show(($rootScope.tipMessage = res.msg));
				 if(checkPhone("iphone")){
					window.webkit.messageHandlers.saveUserPass.postMessage(reqeustData.username+"#"+reqeustData.password);
                }
            }
        });
    }
    
    $scope.fillUser = function(){
    	$scope.user = {};
    	$scope.user.username = localStorage.getItem("username");
    	$scope.user.password = localStorage.getItem("password");
    }

    //非空验证
    $scope.validData = function () {
        if (!$scope.user.username) {
            $rootScope.tipMessage = '邮箱为空'
            $rootScope.tipPopups.show({closeFlag:false});
            return false;
        }
        if (!$scope.user.password) {
            $rootScope.tipPopups.show(($rootScope.tipMessage = '密码为空'));
            return false;
        }
        return true;
    }

    function init(){
//    	var token = getToken();
//    	if(token){
//	    	var type = sessionStorage.getItem("type");
//	    	if(type){
//		    	if(1==type){
//		            $state.go(one.stateName);
//		        }else if(2==type){
//		            $state.go(pv1_0.stateName);
//		        }else{
//		            $state.go(choosePage.stateName);
//		        }
//	    	}else{
//	    		sessionStorage.clear();
//	    		localStorage.clear();
//	            $state.go(loginPage.stateName);
//	    	}
//    	}
    	
        //初始化input插件
        document.title='手机登录';
        $scope.user = {};
        
       //获得账号和密码
		if(checkPhone('android')){
		    if(AndroidMethod.getUserPass()) {
		        var info = JSON.parse(AndroidMethod.getUserPass());
                if(info && info.username){
                    var userInfo = JSON.parse(AndroidMethod.getUserPass());
                    $scope.user.username = userInfo.username;
                    $scope.user.password = userInfo.password;
                }
            }
		}else if(checkPhone("iphone")) {
           //$scope.fillUser();
		    window.webkit.messageHandlers.iosUserInfo.postMessage("");
            // context.ocr("sdmOcrResult");
//			alert(context);
//            if(context && context.getUserPass()) {
//                alert(1);
//                var info = JSON.parse(context.getUserPass());
//                alert("get-2-"+JSON.stringify(info));
//                if(info && info.username){
//                    // alert("get-3-"+info.username);
//                    var userInfo = JSON.parse(context.getUserPass());
//                    $scope.user.username = userInfo.username;
//                    $scope.user.password = userInfo.password;
//                }
//            }
			
			//$scope.user.username = userInfo.username;
            //$scope.user.password = userInfo.password;

        }
    }

    //检查版本信息
    function checkVersion() {
        $scope.versionFlag = true;   //默认需要下载更新app

        $rootScope.tipPopups.show(($rootScope.tipMessage = "FEC建议使用安卓8.1.0及以上," +
            "\ IOS 13.0.0及以上版本.\ " +
            "如版本过低,影响某些功能正常使用"));

        var reqeustData = {};
        $scope.updateInfo = "服务器异常，稍后尝试";
        getHttp($http, 'getAppVersion', reqeustData, function (res) {
            if (res.msg === 'success') {
                var version = res.data.version;
                //version = "1.0";
                $scope.updateInfo = res.data.updateInfo;
                var info = "";
                if(checkPhone('android')){
                    info = AndroidMethod.checkVersion(version,$scope.updateInfo);
                }else if(checkPhone("iphone")) {
                	//info = context.checkVersion(version,$scope.updateInfo);
                }
                if(info){
                    $rootScope.tipPopups.show(($rootScope.tipMessage = info));
                    return;
                }else{
                    $scope.versionFlag = false; //更新app=false
                    init();
                }

            } else {
                $scope.versionFlag = false; //更新app=false
                init();
            }
        });
    }

    checkVersion();
    //init();

});

function iosUserInfo(userInfo) {
    var $scope = getScope("loginController");
    $scope.user.username = userInfo.email;
    $scope.user.password = userInfo.password;
}
